#include<sys/un.h>
#include<sys/socket.h>
#include<string.h>
#include<assert.h>
#include<stdio.h>
#include<errno.h>
#include<unistd.h>
#include <stdlib.h>
#define SV_SOCK_PATH "/tmp/us_xfr"

#define BUF_SIZE 10
#define BACKLOG 5
int main(int argc ,char ** argv)
{
    if(remove(SV_SOCK_PATH) == -1 && errno != ENOENT)
        puts("remove error"),exit(1);
    struct sockaddr_un addr;
    memset(&addr,0,sizeof(struct sockaddr_un));
    addr.sun_family = AF_UNIX;
    
    strncpy(addr.sun_path,SV_SOCK_PATH,sizeof(addr.sun_path) - 1);
    // 打开文件描述符
    int sfd = socket(AF_UNIX,SOCK_STREAM,0);
    assert(sfd != -1);


    assert(bind(sfd,(struct sockaddr*)&addr,sizeof(struct sockaddr_un)) != -1);

    char buf[BUF_SIZE];
    
    assert(listen(sfd,BACKLOG) != -1); // 等待连接
    // 已经建立连接了
    while(1){ 
        int numRead = 0;
        int cfd = accept(sfd,NULL,NULL); // 创建新文件描述符
       
        while((numRead = read(cfd,buf,BUF_SIZE) ) > 0) // 读取
            assert(write(STDOUT_FILENO,buf,numRead) == numRead);
        assert(numRead != - 1);
        assert(close(cfd) != -1);
    }

    return 0;
}